package com.dykj.fireFighting.server.elasticsearch.devHisSs.service.impl;

import com.dykj.fireFighting.api.elasticsearch.devHisSs.entity.EsDevHisSs;
import com.dykj.fireFighting.api.elasticsearch.devHisSs.service.EsDevHisSsService;
import com.dykj.fireFighting.common.core.utils.DataCleaningUtil;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.server.elasticsearch.devHisSs.repository.EsDevHisSsRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zly
 * @Description : 盛善
 * @Date: 2021-11-30
 */
@Slf4j
@DubboService
public class EsDevHisSsServiceImpl implements EsDevHisSsService {
    @Autowired
    private EsDevHisSsRepository esDevHisSsRepository;
    @Override
    public EsDevHisSs importAll(EsDevHisSs esDevHisSs) {
        return esDevHisSsRepository.save(esDevHisSs);
    }

    @Override
    public PageVo<EsDevHisSs> queryList(String sj, String status, String unitId,String centerUnitId,
                                      Integer pageNum, Integer pageSize, String devName, String devType) {

        Pageable pageable = PageRequest.of(pageNum-1, pageSize);
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();

        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        if(null != unitId && StringUtils.isNotBlank(unitId)){
            boolQuery.must(QueryBuilders.termQuery("unitId", unitId));
        }
        if(null != centerUnitId && StringUtils.isNotBlank(centerUnitId)){
            boolQuery.must(QueryBuilders.termQuery("centerUnitId", centerUnitId));
        }
        if (StringUtils.isNotBlank(status)) {
            boolQuery.must(QueryBuilders.termQuery("status", status));
        }
        if (StringUtils.isNotBlank(sj)) {
            boolQuery.must(QueryBuilders.rangeQuery("upTime")
                    .from(sj.split(",")[0])
                    .to(sj.split(",")[1])
                    .includeLower(true)     // 包含上界
                    .includeUpper(true));      // 包含下届
        }
        if (StringUtils.isNotBlank(devType)) {
            boolQuery.must(QueryBuilders.termQuery("devType", devType));
        }

        if (StringUtils.isNotBlank(devName)) {
            boolQuery.must(QueryBuilders.wildcardQuery("devName", "*" + devName + "*"));
        }
        nativeSearchQueryBuilder.withQuery(boolQuery);
        //分页
        nativeSearchQueryBuilder.withPageable(pageable);
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("ztfssj").order(SortOrder.DESC));
        //nativeSearchQueryBuilder.withSort(SortBuilders.scoreSort().order(SortOrder.DESC));
        NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
        log.info("盛善dev_his_ss DSL:{}", searchQuery.getQuery());
        Page<EsDevHisSs> search = esDevHisSsRepository.search(searchQuery);
        PageVo<EsDevHisSs> page = new PageVo<>();
        page.setTotalCount((int) search.getTotalElements());
        List<EsDevHisSs> paggList = new ArrayList<>();
        for (EsDevHisSs e : search.getContent()) {
            e.setBjztSm(DataCleaningUtil.dataCleaning(e.getBjztSm()));
            paggList.add(e);
        }
        page.setList(paggList);
        return page;

    }

}
